/**
 * 
 */
package com.cr.backbone.ajax.html;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.cr.backbone.action.AppBaseActionSupport;
import com.cr.backbone.ajax.html.bean.UserViewTemplate;
import com.cr.backbone.model.MessageTemplate;
import com.cr.backbone.model.SysFile;
import com.cr.backbone.service.business.sysfile.SysFileService;
import com.cr.backbone.util.UtilsFile;
import com.cr.backbone.util.UtilsTemplateFreemarkerToHtml;
import com.readnote.book.model.UserNote;
import com.readnote.book.service.UserNoteService;
import com.readnote.user.model.User;
import com.readnote.user.model.UserInfo;
import com.readnote.user.service.UserInfoService;

/**
 * @project RaedNote
 * @version 1.0
 * @company 
 * @fileDescribe
 * @fileType AjaxTest AjaxTest.java
 * @author xianghua.hu
 * @date 2012-5-16 下午04:24:43
 */
/**
 * @author huxh
 *
 */
public class AjaxFreemakerFileAction extends AppBaseActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1494499536810408186L;
	
	private UserInfoService userInfoService;
	private SysFileService sysFileService;
	private UserNoteService userNoteService;
	
	private MessageTemplate template;
	private String htmlUrl;
	
	public String input(){
		return INPUT;
	}
	
	public String saveHtmlFile() throws Exception {
		
		// 1  生成文件
		String path = UtilsTemplateFreemarkerToHtml.saveCreateHtmlForSrcObject(null, null);
		
		// 2 保存到服务器
		File file=new File(path);
		File dFile= UtilsFile.saveFileToServer(file, file.getName());
		
		
		// 3 生成sysFile
		String fileTitle=dFile.getName();
		String fileType="html";
		User createUser=getLoginInfo().getUser();//当前登录者
		String createUserName="匿名用户";
		if(createUser !=null && !createUser.isNullRow() ){
			UserInfo uInfo = userInfoService.getUserInfoByUser(createUser);
			createUserName = uInfo.getRealName();
		}
		String createIp=ServletActionContext.getRequest().getLocalAddr();//请求者本地IP
		String remoteIp=ServletActionContext.getRequest().getRemoteAddr();//请求远程IP
		
		//2 将文件保存到服务器并保存SysFile
//		SysFile sf1=saveSysFile(uploadFile, fileTitle ,fileType, createUserName, createUser, createIp);
		SysFile sf=sysFileService.saveSysFile(dFile, fileTitle ,fileType, createUserName, createUser, createIp);
		
		htmlUrl=sf.getFilePath();
		
//		return INPUT;
		return SUCCESS;
	}

	
	
	public String saveHtmlFileByTemplate() throws Exception{
		
		// 1  生成文件
		Long id=13l;
		UserNote obj=userNoteService.get(id);
		UserViewTemplate userViewTemplate =new UserViewTemplate();
		
		//
		userViewTemplate.setPageTitle(obj.getTitle());
		userViewTemplate.setPageKeywords(obj.getTitle());
		userViewTemplate.setPageDescription(obj.getTitle());
		userViewTemplate.setBookTitle(obj.getBook().getTitle());
		userViewTemplate.setPageNumber(obj.getPageNumber().toString());
		userViewTemplate.setTitle(obj.getTitle());
		userViewTemplate.setContent(obj.getContent());
		
		String keys="pageTitle,pageKeywords,pageDescription,bookTitle,pageNumber,title,content";
		String templateValue="";
		String fileTemplatePath="\\WEB-INF\\template\\html\\userNote\\view2.html";
		String realPath  = ServletActionContext.getServletContext().getRealPath("");//root
		fileTemplatePath= realPath+fileTemplatePath;
//		InputStreamReader isr=new InputStreamReader(new InputStream( new File(fileTemplatePath)) );
		FileInputStream isr=new FileInputStream( new File(UtilsFile.getPath(fileTemplatePath)) );
		StringBuffer sBuffer =new StringBuffer();
		byte[] cha = new byte[100] ;
		while (isr.read(cha) != -1 ) {
			sBuffer.append(new String(cha,"GBK"));
			cha= new byte[100] ;
		}
		templateValue = sBuffer.toString();
		System.out.println( templateValue );
		
		MessageTemplate messageTemplate=new MessageTemplate();
		messageTemplate.setTemplateKey(keys);
		messageTemplate.setTemplateValue(templateValue);
		
		Map<String, String> params=new HashMap<String, String>();
		
		params = UtilsTemplateFreemarkerToHtml.initParams(params, keys, userViewTemplate );
		
		
		
		
		String path = UtilsTemplateFreemarkerToHtml.saveCreateHtmlForSrcTemplateAndObject(messageTemplate, params);
		
		// 2 保存到服务器
		File file=new File(path);
		File dFile= UtilsFile.saveFileToServer(file, file.getName());
		
		
		// 3 生成sysFIle
		String fileTitle=dFile.getName();
		String fileType="html";
		User createUser=getLoginInfo().getUser();//当前登录者
		String createUserName="匿名用户";
		if(createUser !=null && !createUser.isNullRow() ){
			UserInfo uInfo = userInfoService.getUserInfoByUser(createUser);
			createUserName = uInfo.getRealName();
		}
		String createIp=ServletActionContext.getRequest().getLocalAddr();//请求者本地IP
		String remoteIp=ServletActionContext.getRequest().getRemoteAddr();//请求远程IP
		
		//2 将文件保存到服务器并保存SysFile
//		SysFile sf1=saveSysFile(uploadFile, fileTitle ,fileType, createUserName, createUser, createIp);
		SysFile sf=sysFileService.saveSysFile(dFile, fileTitle ,fileType, createUserName, createUser, createIp);
		
		htmlUrl=sf.getFilePath();
		
//		return INPUT;
		return SUCCESS;
	}
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public SysFileService getSysFileService() {
		return sysFileService;
	}

	public void setSysFileService(SysFileService sysFileService) {
		this.sysFileService = sysFileService;
	}

	public MessageTemplate getTemplate() {
		return template;
	}

	public void setTemplate(MessageTemplate template) {
		this.template = template;
	}

	public String getHtmlUrl() {
		return htmlUrl;
	}

	public void setHtmlUrl(String htmlUrl) {
		this.htmlUrl = htmlUrl;
	}

	public UserNoteService getUserNoteService() {
		return userNoteService;
	}

	public void setUserNoteService(UserNoteService userNoteService) {
		this.userNoteService = userNoteService;
	}
	
}
 